package site.shader85.OICQBind;


import java.io.File;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import static site.shader85.OICQBind.Utils.*;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;


public class BindPlugin extends JavaPlugin{
		
	public static YamlConfiguration stmp = null;
	public static YamlConfiguration registeredConfig = null;
	File file1 = new File(getDataFolder(),"players.yml");
	ChattingTimer ct = new ChattingTimer();
	public static YamlConfiguration lang = null;
	File cfg = new File(getDataFolder(),"config.yml");
	public static HashMap<Player,Integer> id = new HashMap<Player, Integer>();
	public static HashMap<Player,String> tempQQ = new HashMap<Player, String>();
	/////////////////////////////语言
	public static String host = "smtp.126.com";
	public static String user = "pa30638656@126.com";
	public static String password = "zhouqianyi";
	public String email_title = "[服务器娘]这里是你的验证码";
	private String title = "&9[QQ绑定系统]";
	private String parse_error = "§a参数错误,请重新输入";
	private String console_error = "控制台不能使用这个指令";
	private String player_error = "玩家不能使用这个指令";
	private String bind_error = "§a验证码错误，请重新输入";
	private String send_success = "§a发送成功！";
	private String bind_success = "§a绑定成功!";
	private String has_send = "§a已经发送过验证码，如果未收到，请重新进入服务器后重新输入该指令";
	private String has_bind = "§a你已经完成过绑定了";
	private String havnt_bind = "§a你尚未发送验证码,请先完成第一步";
	public static String kickmessage = "长时间未能完成QQ绑定";
	public static List<String> message = new ArrayList<String>();
	@Override
	public void onEnable() {
		try{
			Class.forName("site.shader85.OICQBind.Utils");
			Class.forName("org.apache.poi.hssf.usermodel.HSSFWorkbook");
		}catch(Exception e){e.printStackTrace();}
		loadFile();
		getServer().getPluginManager().registerEvents(new BindingListener(), this);
		ct.runTaskTimer(this, 1, 20*5);
	}

	@Override
	public void onDisable() {
		
	}
	
	public void loadFile(){
		if(!cfg.exists())
			saveDefaultConfig();
		stmp = YamlConfiguration.loadConfiguration(cfg);
		
		title = stmp.getString("title");
		email_title = stmp.getString("email_title");
		parse_error = stmp.getString("parse_error");
		console_error = stmp.getString("console_error");
		player_error = stmp.getString("player_error");
		bind_error = stmp.getString("bind_error");
		send_success = stmp.getString("send_success");
		bind_success = stmp.getString("bind_success");
		has_send = stmp.getString("has_send");
		has_bind = stmp.getString("has_bind");
		havnt_bind = stmp.getString("havnt_bind");
		kickmessage = stmp.getString("kickmessage");
		message = stmp.getStringList("message");
		try{
		registeredConfig = YamlConfiguration.loadConfiguration(file1);
		registeredConfig.save(file1);
		}
		catch(Exception e){Bukkit.getLogger().info("文件配置出错");}

	}
	
	public void saveFile(){
		try{
			registeredConfig.save(file1);
		}
		catch(Exception e){Bukkit.getLogger().info("文件配置出错");}
	}
	
	@Override
	public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
		if(command.getName().equalsIgnoreCase("qbind")){
			if(sender instanceof Player){
				if(BindingListener.unregisteredPlayers.containsKey(sender)){
					if(args.length == 1){
						if(isQQ(args[0])){
							if(id.get(sender)==null){
								int uid = send(args[0]);
								id.put((Player)sender,uid);
								tempQQ.put((Player)sender, args[0]);
								sender.sendMessage(title+"§r"+send_success);
							}
							else
								sender.sendMessage(title+"§r"+has_send);
						}
						else
							sender.sendMessage(title+"§r"+parse_error);
					}
					else 
						sender.sendMessage(title+"§r"+parse_error);
				}
				else
					sender.sendMessage(title+"§r"+has_bind);
			}
			else
				sender.sendMessage(console_error);
			return true;
		}
		if(command.getName().equalsIgnoreCase("qconfirm")){
			if(sender instanceof Player){
				if(BindingListener.unregisteredPlayers.containsKey(sender)){
					if(args.length == 1){
						Integer qid = id.get(sender);
						if(qid != null){
							if(args[0].equals(qid.toString())){
								BindingListener.unregisteredPlayers.remove(sender);
								registeredConfig.set(sender.getName(),tempQQ.get(sender));
								tempQQ.remove(sender);
								saveFile();
								sender.sendMessage(title+"§r"+bind_success);
							}
							else
								sender.sendMessage(title+"§r"+bind_error);
						}
						else
							sender.sendMessage(title+"§r"+havnt_bind);
					}
					else 
						sender.sendMessage(title+"§r"+parse_error);
				}
				else 
					sender.sendMessage(title+"§r"+has_bind);
			}
			else
				sender.sendMessage(console_error);
			return true;
		}
		if(command.getName().equalsIgnoreCase("qexport")){
			if(!(sender instanceof Player)){
				DeamonWriter dw = new DeamonWriter(registeredConfig, this);
				Thread t = new Thread(dw);
				t.setDaemon(true);
				t.start();
			}
			else
				sender.sendMessage(title+"§r"+player_error);
		}
		return false;
	}
}